<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="员工工号" prop="staffNo">
        <el-input v-model="queryParams.staffNo" placeholder="请输入工号" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="员工姓名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入姓名" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="证书编号" prop="certNumber">
        <el-input v-model="queryParams.certNumber" placeholder="请输入证书编号" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="项目代码" prop="projectCode">
        <el-input v-model="queryParams.projectCode" placeholder="请输入项目代码" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="证书类型" prop="getCertStatus">
        <el-select v-model="queryParams.getCertStatus" placeholder="取证状态" clearable @clear="clearGetCertStatus" size="small">
          <el-option v-for="dict in getCertStatusOptions"
            :key="dict.code"
            :label="dict.name"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="证书状态" prop="certStatus">
        <el-select v-model="queryParams.certStatus" placeholder="证书状态" @clear="clearCertStatus" clearable size="small">
          <el-option v-for="dict in certStatusOptions"
            :key="dict.code"
            :label="dict.name"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="到期日期">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px"
          value-format="yyyy-MM-dd" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item style="margin-left: 10px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" style="margin-left: 10px;" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleRevoke" v-hasPermi="['CertInfo:revoke']"
        >吊销</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-circle-plus" size="mini" :disabled="multiple"
                @click="handlePostpone" v-hasPermi="['CertInfo:postpone']"
        >延期</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-printer" size="mini" :disabled="multiple"
                @click="handlePrinter" v-hasPermi="['CertInfo:print']"
        >打印</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-upload2" size="mini"
          @click="handleImport" v-hasPermi="['CertInfo:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini"
          @click="handleExport" v-hasPermi="['CertInfo:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--  max-height="400"  -->
    <el-table v-loading="loading" :data="certList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="工号" align="center" prop="staffNo" :show-overflow-tooltip="true"/>
      <el-table-column label="姓名" align="center" prop="username" :show-overflow-tooltip="true"/>
      <el-table-column label="部门" align="center" prop="deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="编号" align="center" prop="certNumber" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="证书名称" align="center" prop="certName" :show-overflow-tooltip="true"/>-->
      <el-table-column label="项目代码" align="center" prop="projectCode" :show-overflow-tooltip="true"/>
      <el-table-column label="发证日期" align="center" prop="obtainEvidenceDate" width="100px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.obtainEvidenceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="到期日期" align="center" prop="endDate" width="100px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="有效期（年）" align="center" prop="reviewPeriod" />
      <el-table-column label="证书类型" align="center" prop="getCertStatus" :formatter="getStatusFormat" />
      <el-table-column label="证书来源" align="center" prop="institutionName" :show-overflow-tooltip="true" />
      <el-table-column label="证书状态" align="center" prop="certStatus" :formatter="certStatusFormat" />
      <el-table-column label="操作" align="center" width="180" class-name="small-padding">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view"
                     @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-view"
                     @click="handleCert(scope.row)"
          >电子证书</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
            v-show="total>0"
            :total="total"
            :page.sync="pageParams.page"
            :limit.sync="pageParams.rows"
            @pagination="getList"
    />

    <!-- 证书吊销信息 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="80%" append-to-body>
      <revoke-form :revokeForm="revokeForm" @submitForm="getList" @cancelDialog="cancel"></revoke-form>
    </el-dialog>

    <!-- 证书延期信息 -->
    <el-dialog :title="title" :visible.sync="postponeOpen" v-if="postponeOpen" width="80%" append-to-body>
      <postpone-form :postponeForm="postponeForm" @submitForm="getList" @cancelDialog="cancel"></postpone-form>
    </el-dialog>

    <!-- 证书详情信息 -->
    <el-dialog :title="title" :visible.sync="certDetailOpen" v-if="certDetailOpen" width="55%" append-to-body>
      <cert-detail-form :assessTypes="assessTypes" :getCertStatusOptions="getCertStatusOptions" :certStatusOptions="certStatusOptions" :certInfoForm="certInfoForm" @cancelDialog="cancel"></cert-detail-form>
    </el-dialog>

    <!-- 证书导入对话框 -->
    <el-dialog :title="uploadTitle" :visible.sync="uploadOpen" width="400px" append-to-body>
      <ExcelImport :uploadUrl="uploadUrl" @importTemplate="importTemplateCallback" @submitBtnSuccess="getList" @cancelDialog="handleImportCancle"/>
    </el-dialog>
  </div>
</template>

<script>
  import {deleteCert, exportCert, importTemplate, listCertInfo} from "@/api/cert/certInfo";
  import revokeForm from './form/revokeForm'
  import postponeForm from './form/postponeForm'
  import certDetailForm from './form/certDetailForm'
  import ExcelImport from "@/components/tools/ExcelImport";

  export default {
    name: "certInfo",
    components: {
      revokeForm,
      postponeForm,
      certDetailForm,
      ExcelImport
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 分页参数
        pageParams: {
          page: 1,
          rows: 10,
        },
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          staffNo: undefined,
          username: undefined,
          deptName: undefined,
          certNumber: undefined,
          projectCode: undefined,
          getCertStatus: undefined,
          certSource: undefined,
          certStatus: undefined
        },
        // 证书表格数据
        certList: [],
        // 吊销表单参数
        revokeForm: {
          // 吊销列表
          certInfoList: [],
          cause: ''
        },
        // 延期表单参数
        postponeForm: {
          // 吊销列表
          certInfoList: [],
          cause: ''
        },
        // 弹出层标题
        title: "",
        // 是否吊销显示弹出层
        open: false,
        // 是否显示延期弹出层
        postponeOpen: false,
        // 是否显示证书详情
        certDetailOpen: false,
        // 详情表单
        certInfoForm: {},
        // 证书类型数据字典
        getCertStatusOptions: [],
        // 证书状态数据字典
        certStatusOptions: [],
        // 矩阵评估字典
        assessTypes: [],
        // 日期范围
        dateRange: [],
        // 证书导入参数
        uploadUrl: "/api/uf/boostor/exam/certinfo/ExamCertInfoRest/importData/v1",
        // 是否显示弹出层（证书导入）
        uploadOpen: false,
        // 弹出层标题（证书导入）
        uploadTitle: '',

      };
    },
    created() {
      //查询取证状态字典状态
      this.getDicts("cert_get_status").then(response => {
        this.getCertStatusOptions = response.responseBody.result;
      });
      //查询证书字典状态
      this.getDicts("cert_status").then(response => {
        this.certStatusOptions = response.responseBody.result;
      });
      //查询是否矩阵评估字典类型
      this.getDicts("question_project_assess").then(response => {
        this.assessTypes = response.responseBody.result;
      });
      this.getList();
    },
    methods: {
      /** 查询证书列表 */
      getList() {
        this.loading = true;
        listCertInfo({
          'pageParameter': this.pageParams,
          'searchParams': this.addDateRange(this.queryParams, this.dateRange)
        }).then(
          response => {
            this.certList = response.responseBody.result;
            this.total = response.responseBody.pageParameter.totalCount;
            this.loading = false;
          }
        );
      },
      /** 证书详情 */
      handleDetail(row) {
        // this.msgWarning("证书详情！");
        this.certDetailOpen = true;
        this.certInfoForm = row;
      },
      /** 电子证书 */
      handleCert(row) {
        this.msgWarning("电子证书待开发。。！");
        this.certInfoForm = row;
      },
      // 取证状态字典翻译
      getStatusFormat(row, column) {
        return this.selectDictLabel(this.getCertStatusOptions, row.getCertStatus);
      },
      // 证书状态字典翻译
      certStatusFormat(row, column) {
        return this.selectDictLabel(this.certStatusOptions, row.certStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.postponeOpen = false;
        this.certDetailOpen = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.revokeForm.cause = '';
        this.postponeForm.cause = '';
        this.resetForm("form");
      },
      // 清空搜索取证状态
      clearGetCertStatus(val) {
        this.queryParams.getCertStatus = undefined;
      },
      // 清空搜索证书状态
      clearCertStatus(val) {
        this.queryParams.certStatus = undefined;
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.pageParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.dateRange = [];
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.certId)
        this.revokeForm.certInfoList = [];
        this.postponeForm.certInfoList = [];
        this.revokeForm.certInfoList = selection.map(item => item);
        this.postponeForm.certInfoList = selection.map(item => item);
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      // 吊销按钮操作
      handleRevoke() {
        this.title = '吊销申请';
        this.open = true;
      },
      /** 延期按钮操作 */
      handlePostpone(row) {
        this.title = '延期申请';
        this.postponeOpen = true;
      },
      /** 证书打印 */
      handlePrinter(row) {
        this.msgWarning("这是打印。。。。。");
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.certId || this.ids;
        this.$confirm('确认删除已选证书的数据项？','提示',{
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: "warning"
      }).then(() => {
          this.loading = true;
          deleteCert(ids).then(response => {
            this.msgSuccess("删除成功");
            this.getList();
          })
        }).catch((err) => {
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导入按钮操作 */
      handleImport() {
        this.uploadTitle = "外部证书导入";
        this.uploadOpen = true;
      },
      handleImportCancle() {
        this.uploadOpen = false;
      },
      /** 下载模板操作 */
      importTemplateCallback(callback) {
        importTemplate().then(response => {
          callback(response.responseBody)
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('确认导出所有证书数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          exportCert({searchParams: queryParams}).then(response => {
            this.download(response.responseBody);
          })
        }).catch(() => {})
      },
    }
  };
</script>
